package models.saleman;

import activity.give.GiveWebUserAuth;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.weixin.WebUser;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 业务员任务表
 */

@Entity
@Table(name = "sale_man_tasks")
public class SaleManTask extends BaseModel {


    /**
     * 业务员
     */
    @JoinColumn(name = "sale_man_id")
    @ManyToOne
    public GiveWebUserAuth saleMan;


    /**
     * 任务日期
     */
    @Column(name = "task_date")
    @Temporal(TemporalType.DATE)
    public Date taskDate;
    /**
     * 任务日期
     */
    @Transient
    public String taskDateStr;

    /**
     * 任务总数量
     */
    @Column(name = "task_num")
    public Integer taskNum = 0;

    /**
     * 已拜访客户数量
     */
    @Column(name = "visit_num")
    public Integer visitNum = 0;


    /**
     * 删除状态
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 备注
     */
    @Column(name = "remark")
    public String remark;

    @Transient
    public int taskIndex;

    /**
     * @param id
     * @return
     */
    public static SaleManTask findByTaskId(long id) {
        return SaleManTask.find("deleted = ?1 and id = ?2", DeletedStatus.UN_DELETED, id).first();
    }

    /**
     * 根据业务员和拜访日期查询 拜访任务
     *
     * @param saleManUserId
     * @param taskDate
     * @return
     */
    public static SaleManTask findBySaleManAndTaskDate(long saleManUserId, Date taskDate) {
        return SaleManTask.find("deleted = ?1 and saleMan.webUser.id = ?2 and taskDate = ?3", DeletedStatus.UN_DELETED, saleManUserId, taskDate).first();
    }


}
